<html>
<head><meta charset="utf-8"><title>Test for naked args · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Test.20for.20naked.20args.html">Test for naked args</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="247973567"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Test%20for%20naked%20args/near/247973567" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathaniel McCallum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Test.20for.20naked.20args.html#247973567">(Aug 02 2021 at 21:25)</a>:</h4>
<p><span class="user-mention" data-user-id="143274">@Amanieu</span>  I want to add a compilation test for certain lint behavior on <code>#[naked]</code> functions. I've created a function in the rust playground that I want to add a test for.</p>
<p><a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=2acfa86af710553d3a6583b41ba2f0f4">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=2acfa86af710553d3a6583b41ba2f0f4</a></p>
<p>However, when I copy/paste the function into <code>src/test/ui/asm/naked-functions.rs</code>, none of the errors actually occur when I run the test. Further, one of them permits inline asm without an unsafe block; which seems... unsafe. I'm pretty sure there's something about test execution that I'm missing. But I'm struggling at this point to figure out what.</p>



<a name="248193094"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Test%20for%20naked%20args/near/248193094" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Test.20for.20naked.20args.html#248193094">(Aug 03 2021 at 08:10)</a>:</h4>
<p>That's strange, we have a ui test that specifically checks that <code>asm!</code> without unsafe is rejected.</p>



<a name="248215746"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Test%20for%20naked%20args/near/248215746" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> fee1-dead <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Test.20for.20naked.20args.html#248215746">(Aug 03 2021 at 12:49)</a>:</h4>
<p>this might be because previous errors prevent the errors you expect from emitting.</p>



<a name="248220868"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Test%20for%20naked%20args/near/248220868" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathaniel McCallum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Test.20for.20naked.20args.html#248220868">(Aug 03 2021 at 13:32)</a>:</h4>
<p><span class="user-mention" data-user-id="143274">@Amanieu</span> I presume you're talking about <code>src/test/ui/unsafe/inline_asm.rs</code></p>



<a name="248229192"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Test%20for%20naked%20args/near/248229192" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathaniel McCallum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Test.20for.20naked.20args.html#248229192">(Aug 03 2021 at 14:33)</a>:</h4>
<p><span class="user-mention" data-user-id="361356">@fee1-dead</span> The previous errors would show up in the test output. I tried introducing another error manually before the function and I see the manual error clearly in the output.</p>



<a name="248231245"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Test%20for%20naked%20args/near/248231245" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathaniel McCallum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Test.20for.20naked.20args.html#248231245">(Aug 03 2021 at 14:48)</a>:</h4>
<p>Here are my changes:</p>
<div class="codehilite"><pre><span></span><code>$ git diff
diff --git a/src/test/ui/asm/naked-functions.rs b/src/test/ui/asm/naked-functions.rs
index 7075995c2cf..6ce086f4648 100644
--- a/src/test/ui/asm/naked-functions.rs
+++ b/src/test/ui/asm/naked-functions.rs
@@ -210,3 +210,18 @@ pub extern &quot;C&quot; fn valid_b() {
 pub unsafe extern &quot;C&quot; fn inline_all() {
     asm!(&quot;&quot;, options(noreturn));
 }
+
+#[forbid(unused_variables)]
+pub extern &quot;sysv64&quot; fn unused_arguments(a: usize, b: usize) -&gt; usize {
+    //~^ ERROR unused variable: `a`
+    //~| ERROR unused variable: `b`
+    //unsafe {
+        asm!(
+            &quot;mov rax, rdi&quot;,
+            &quot;add rax, rsi&quot;,
+            &quot;ret&quot;,
+            options(noreturn)
+        );
+        //~^ ERROR use of inline assembly is unsafe and requires unsafe function or block
+    //}
+}
</code></pre></div>



<a name="248231461"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Test%20for%20naked%20args/near/248231461" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathaniel McCallum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Test.20for.20naked.20args.html#248231461">(Aug 03 2021 at 14:50)</a>:</h4>
<p>Here is my output:</p>
<div class="codehilite"><pre><span></span><code>$ ./x.py test src/test/ui/asm/naked-functions.rs
...
---- [ui] ui/asm/naked-functions.rs stdout ----

error: /home/npmccallum/rust/src/test/ui/asm/naked-functions.rs:215: expected error not found: unused variable: `a`

error: /home/npmccallum/rust/src/test/ui/asm/naked-functions.rs:215: expected error not found: unused variable: `b`

error: /home/npmccallum/rust/src/test/ui/asm/naked-functions.rs:224: expected error not found: use of inline assembly is unsafe and requires unsafe function or block

error: 0 unexpected errors found, 3 expected errors not found
status: exit status: 1
command: &quot;/home/npmccallum/rust/build/x86_64-unknown-linux-gnu/stage1/bin/rustc&quot; &quot;/home/npmccallum/rust/src/test/ui/asm/naked-functions.rs&quot; &quot;-Zthreads=1&quot; &quot;--target=x86_64-unknown-linux-gnu&quot; &quot;--error-format&quot; &quot;json&quot; &quot;-Ccodegen-units=1&quot; &quot;-Zui-testing&quot; &quot;-Zdeduplicate-diagnostics=no&quot; &quot;-Zemit-future-incompat-report&quot; &quot;--emit&quot; &quot;metadata&quot; &quot;-C&quot; &quot;prefer-dynamic&quot; &quot;--out-dir&quot; &quot;/home/npmccallum/rust/build/x86_64-unknown-linux-gnu/test/ui/asm/naked-functions&quot; &quot;-A&quot; &quot;unused&quot; &quot;-Crpath&quot; &quot;-O&quot; &quot;-Cdebuginfo=0&quot; &quot;-Lnative=/home/npmccallum/rust/build/x86_64-unknown-linux-gnu/native/rust-test-helpers&quot; &quot;-L&quot; &quot;/home/npmccallum/rust/build/x86_64-unknown-linux-gnu/test/ui/asm/naked-functions/auxiliary&quot;
not found errors (from test file): [
    Error {
        line_num: 215,
        kind: Some(
            Error,
        ),
        msg: &quot;unused variable: `a`&quot;,
    },
    Error {
        line_num: 215,
        kind: Some(
            Error,
        ),
        msg: &quot;unused variable: `b`&quot;,
    },
    Error {
        line_num: 224,
        kind: Some(
            Error,
        ),
        msg: &quot;use of inline assembly is unsafe and requires unsafe function or block&quot;,
    },
]
</code></pre></div>



<a name="248231601"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Test%20for%20naked%20args/near/248231601" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathaniel McCallum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Test.20for.20naked.20args.html#248231601">(Aug 03 2021 at 14:51)</a>:</h4>
<p>Now the <code>-A unused</code> in the command could be relevant. But I'm explicitly overriding that with the attribute. It also doesn't explain the missing unsafe error.</p>



<a name="248231615"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Test%20for%20naked%20args/near/248231615" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tm <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Test.20for.20naked.20args.html#248231615">(Aug 03 2021 at 14:51)</a>:</h4>
<p>Unsafety checking happens later (MIR / THIR?). I wouldn't be surprised if other errors stop the compilation before that.</p>



<a name="248231945"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Test%20for%20naked%20args/near/248231945" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathaniel McCallum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Test.20for.20naked.20args.html#248231945">(Aug 03 2021 at 14:53)</a>:</h4>
<p><span class="user-mention" data-user-id="352985">@tm</span> That could answer the unsafe question. I don't <em>need</em> a fix for the unsafe stuff. I'm only using it as a (probably bad) proxy for why the unused stuff isn't working.</p>



<a name="248232284"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Test%20for%20naked%20args/near/248232284" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Test.20for.20naked.20args.html#248232284">(Aug 03 2021 at 14:55)</a>:</h4>
<p><span class="user-mention silent" data-user-id="352985">tm</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/Test.20for.20naked.20args/near/248231615">said</a>:</p>
<blockquote>
<p>Unsafety checking happens later (MIR / THIR?). I wouldn't be surprised if other errors stop the compilation before that.</p>
</blockquote>
<p>(Still MIR by default today, THIR unsafeck is behind a <code>-Z</code> flag)</p>



<a name="248232285"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Test%20for%20naked%20args/near/248232285" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathaniel McCallum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Test.20for.20naked.20args.html#248232285">(Aug 03 2021 at 14:55)</a>:</h4>
<p>But I need to produce the <code>unused_variables</code> error so that when I disable the error in this particular case I know that I've succeeded.</p>



<a name="248233088"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Test%20for%20naked%20args/near/248233088" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tm <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Test.20for.20naked.20args.html#248233088">(Aug 03 2021 at 15:01)</a>:</h4>
<p>The situation with unused variables lint is similar: this high level structure of passes: <a href="https://github.com/rust-lang/rust/blob/2939249f294dd54a9ce78a8ee1f2922a44e7fb7c/compiler/rustc_interface/src/passes.rs#L854-L911">https://github.com/rust-lang/rust/blob/2939249f294dd54a9ce78a8ee1f2922a44e7fb7c/compiler/rustc_interface/src/passes.rs#L854-L911</a><br>
In between <code>check_mod_naked_functions</code> and <code>check_mod_liveness</code> (emits warnings about unused variables) there is <code>typeck::check_crate(tcx)?;</code> which will exit if there had been any errors reported.</p>



<a name="248233162"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Test%20for%20naked%20args/near/248233162" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tm <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Test.20for.20naked.20args.html#248233162">(Aug 03 2021 at 15:01)</a>:</h4>
<p>You will need a separate UI test.</p>



<a name="248233436"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Test%20for%20naked%20args/near/248233436" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathaniel McCallum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Test.20for.20naked.20args.html#248233436">(Aug 03 2021 at 15:03)</a>:</h4>
<p><span class="user-mention" data-user-id="352985">@tm</span> I don't understand what the separate UI test accomplishes.</p>



<a name="248233542"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Test%20for%20naked%20args/near/248233542" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathaniel McCallum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Test.20for.20naked.20args.html#248233542">(Aug 03 2021 at 15:04)</a>:</h4>
<p>Oh, wait. The problem is that there can't be any other errors in the file. Correct?</p>



<a name="248233966"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Test%20for%20naked%20args/near/248233966" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tm <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Test.20for.20naked.20args.html#248233966">(Aug 03 2021 at 15:07)</a>:</h4>
<p>Yes, exactly. Any other errors from an earlier compilation stage would be problematic.</p>



<a name="248234171"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Test%20for%20naked%20args/near/248234171" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathaniel McCallum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Test.20for.20naked.20args.html#248234171">(Aug 03 2021 at 15:09)</a>:</h4>
<p><span class="user-mention" data-user-id="352985">@tm</span> Now I have this code:</p>
<div class="codehilite"><pre><span></span><code>$ git diff HEAD
diff --git a/src/test/ui/asm/naked-functions-unused.rs b/src/test/ui/asm/naked-functions-unused.rs
new file mode 100644
index 00000000000..ce4ce7433d3
--- /dev/null
+++ b/src/test/ui/asm/naked-functions-unused.rs
@@ -0,0 +1,19 @@
+// only-x86_64
+#![warn(unused)]
+#![feature(asm)]
+#![feature(naked_functions)]
+#![crate_type = &quot;lib&quot;]
+
+#[naked]
+pub extern &quot;sysv64&quot; fn unused_arguments(a: usize, b: usize) -&gt; usize {
+    //~^ WARN unused variable: `a`
+    //~| WARN unused variable: `b`
+    unsafe {
+        asm!(
+            &quot;mov rax, rdi&quot;,
+            &quot;add rax, rsi&quot;,
+            &quot;ret&quot;,
+            options(noreturn)
+        );
+    }
+}
</code></pre></div>



<a name="248234621"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Test%20for%20naked%20args/near/248234621" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tm <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Test.20for.20naked.20args.html#248234621">(Aug 03 2021 at 15:12)</a>:</h4>
<p>UI tests by default expected to fail, while this succeeds. So it will need either a <code>#[deny(expected-lint)]</code>, to turn warnings into errors, or <code>// check-pass</code> annotation.</p>



<a name="248234860"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Test%20for%20naked%20args/near/248234860" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathaniel McCallum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Test.20for.20naked.20args.html#248234860">(Aug 03 2021 at 15:13)</a>:</h4>
<p><span class="user-mention" data-user-id="352985">@tm</span> I got it!</p>



<a name="248234893"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Test%20for%20naked%20args/near/248234893" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathaniel McCallum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Test.20for.20naked.20args.html#248234893">(Aug 03 2021 at 15:14)</a>:</h4>
<p><span class="user-mention" data-user-id="352985">@tm</span> Thank you so much!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>